package com.ease.map.control {
   import com.ease.map.Map;
   import com.ease.map.common.util.Util;
   import com.ease.map.control.ui.ImageButton;
   import com.ease.map.events.FeatureEvent;
   import com.ease.map.events.LayerEvent;
   import com.ease.map.events.MapEvent;
   import com.ease.map.geometry.basetype.Pixel;
   import com.ease.map.geometry.basetype.Size;

   import flash.display.DisplayObject;
   import flash.events.Event;
   import flash.events.MouseEvent;
   import flash.text.TextField;
   import flash.text.TextFormat;
   import com.ease.map.control.PanZoom;

   /**
    * As the PanZoom control, it allows to pan and zoom in/out the map.
    * It adds a vertical slider for zooming functionality
    */
   public class PanZoomBar extends PanZoom {

      private var _zoomStopWidth:Number = 12;

      private var _zoomStopHeight:Number = 7;

      private var _slider:DisplayObject = null;

      private var _zoomBar:DisplayObject = null;

      private var _startTop:Number = NaN;

      private var _mouseDragStart:Pixel = null;

      private var _zoomStart:Pixel = null;

      private var label:TextField = null;

      public function PanZoomBar(position:Pixel = null) {
         super(position);
      }

      override public function destroy():void {
         this.removeChild(this.slider);
         this.slider = null;

         this.removeChild(this.zoomBar);
         this.zoomBar = null;

         this.removeChild(this.label);
         this.label = null;

         this.map.removeEventListener(MapEvent.MOVE_END, this.moveZoomBar);

         super.destroy();
      }


      /**
       * Get the existing map
       *
       * @param value
       */
      override public function set map(map:Map):void {
         super.map = map;
      }

      /**
       * 清除已有的child
       *
       * @param obt
       */
      private function clearAll():void {
         if (this != null) {
            this.graphics.clear();
            var i:int = this.numChildren - 1;
            for (i; i > -1; i--) {
               removeChildAt(i);
            }
         }
      }

      /**
       * Draw buttons for zoom and pan of the map
       */
      override public function draw():void {
         this.clearAll();
         this.buttons = new Vector.<ImageButton>();
         var px:Pixel = this.position;
         var sz:Size = new Size(18, 18);
         var sz2:Size = new Size(14, 8);
         var sz3:Size = new Size(9, 13);
         var centered:Pixel = new Pixel(this.x+10 , this.y+10);

         this.graphics.lineStyle(1, 0x8F949F, 1.0, false, "normal", null, null, 3);
         this.graphics.beginFill(0xFFFFFF, 1.0);
         this.graphics.drawCircle(centered.x,centered.y,20);

         this._addLabel();
         this._addButton("panup", new northMiniImg(), centered.add(-7,-sz.h/2-8), sz2, "Pan Up");
         this._addButton("panleft", new westMiniImg(), centered.add(-sz.w/2-9,-6.5), sz3, "Pan Left");
         this._addButton("panright", new eastMiniImg(), centered.add(sz.w/2,-6.5), sz3, "Pan Right");
         this._addButton("pandown", new southMiniImg(), centered.add(-7, sz.h/2), sz2, "Pan Down");

         this._addButton("zoomworld", new zoomWorldMiniImg(), centered.add(-sz.w/2, -sz.h/2), sz, "Center");

         this._addButton("zoomin", new zoomPlusMiniImg(), centered.add(-sz.w/2, sz.h /2*3-4 ), sz, "Zoom In");
         centered = this._addZoomBar(centered.add(-sz.w/2, sz.h /2*5-4 ));
         this._addButton("zoomout", new zoomMinusMiniImg(), centered, sz, "Zoom Out");

      }

      /**
       * Add zoom bar (slider)
       *
       * @param centered
       *
       * @return Pixel
       */
      public function _addZoomBar(centered:Pixel):Pixel {

         var zoomsToEnd:int = this.map.numZoomLevels - 1 - this.map.zoom;
         var sz:Size = new Size(18, 6);

         this.slider = new ImageButton("slider", new sliderImg(), new Pixel(centered.x , centered.y + zoomsToEnd * this.zoomStopHeight), sz);

         slider.addEventListener(MouseEvent.MOUSE_DOWN, this.zoomBarDown);
         slider.addEventListener(MouseEvent.MOUSE_UP, this.zoomBarUp);
         slider.addEventListener(MouseEvent.MOUSE_OVER, this.zoomBarMouseOver);
         slider.addEventListener(MouseEvent.MOUSE_OUT, this.zoomBarMouseOut);

         var barxy:Pixel = new Pixel(centered.x+3, centered.y);
         this.zoomBar = new ImageButton("zooomBar", new zoombarImg(), barxy, sz);

         zoomBar.width = this.zoomStopWidth;
         zoomBar.height = this.zoomStopHeight * this.map.numZoomLevels;
         zoomBar.addEventListener(MouseEvent.MOUSE_DOWN, this.zoomBarClick);
         zoomBar.addEventListener(MouseEvent.DOUBLE_CLICK, this.doubleClick);
         zoomBar.addEventListener(MouseEvent.CLICK, this.doubleClick);
         zoomBar.addEventListener(MouseEvent.MOUSE_OVER, this.zoomBarMouseOver);
         zoomBar.addEventListener(MouseEvent.MOUSE_OUT, this.zoomBarMouseOut);

         this.addChild(this.zoomBar);
/*         //画刻度
         this.graphics.lineStyle(1, 0x2060c0, 1.0, false, "normal", null, null, 3);
         var star_commands:Vector.<int> = new Vector.<int>();
         var star_coord:Vector.<Number> = new Vector.<Number>();
         for (var j:int = 0; j < this.map.numZoomLevels; j++) {
            star_commands.push(1);
            star_coord.push(centered.x + 5) ;
            star_coord.push(centered.y + this.zoomStopHeight * (j + 0.5) - 1);
            star_commands.push(2);
            star_coord.push(centered.x + 13);
            star_coord.push(centered.y + this.zoomStopHeight * (j + 0.5) - 1);
         }
         this.graphics.drawPath(star_commands, star_coord);*/

         this.startTop = int(this.zoomBar.y);
         this.addChild(slider);

         var newTop:Number = ((this.map.numZoomLevels - 1) - this.map.zoom) * this.zoomStopHeight + this.startTop + 1;
         //显示
         this.label.y = newTop - this.zoomStopHeight / 2 - 3;
         this.label.x = this.slider.x + 18;
         this.label.text = this.map.zoom.toString();

         this.map.addEventListener(MapEvent.MOVE_END, this.moveZoomBar);

         centered = centered.add(0, this.zoomStopHeight * this.map.numZoomLevels);
         return centered;
      }

      private function _addLabel():void {
         this.label = new TextField();
         this.label.width = 20;
         var labelFormat:TextFormat = new TextFormat();
         labelFormat.size = 9;
         labelFormat.color = 0x8F949F;
         labelFormat.font = "微软雅黑";
         this.label.setTextFormat(labelFormat);
         this.label.text = "";

         this.addChild(this.label);
      }

      //Slide Event

      public function passEventToSlider(evt:MouseEvent):void {
         //this.sliderEvents.handleBrowserEvent(evt);
      }

      public function zoomBarClick(evt:MouseEvent):void {
         var y:Number = evt.stageY;
         var top:Number = Util.pagePosition(evt.currentTarget)[1];
         var levels:Number = Math.floor((y - top) / this.zoomStopHeight);
         this.map.zoom = (this.map.numZoomLevels - 1) - levels;
         evt.stopPropagation();
      }

      public function zoomBarDown(evt:MouseEvent):void {
      /*			this.mouseDragStart=new Pixel(map.mouseX, map.mouseY);
         this.zoomStart=new Pixel(evt.stageX, evt.stageY);
         this.useHandCursor=true;
         slider.addEventListener(MouseEvent.MOUSE_MOVE, this.zoomBarDrag);
       evt.stopPropagation();*/
      }

      public function zoomBarDrag(evt:MouseEvent):void {
      /*			if (this.mouseDragStart != null)
         {
         var deltaY:Number=this.mouseDragStart.y - map.mouseY;
         var offsets:Array=Util.pagePosition(this.zoomBar);
         if ((map.mouseY - offsets[1]) > 0 && (map.mouseY - offsets[1]) < int(this.zoomBar.height) - 2)
         {
         var newTop:Number=int(this.slider.y) - deltaY;
         this.slider.y=int(this.slider.y) - deltaY;
         }
         this.mouseDragStart=new Pixel(map.mouseX, map.mouseY);
         evt.stopPropagation();
       }*/
      }

      public function zoomBarUp(evt:MouseEvent):void {
      /*			if (this.zoomStart)
         {
         this.useHandCursor=false;

         var deltaY:Number=this.zoomStart.y - evt.stageY;
         this.map.zoom=this.map.zoom + Math.round(deltaY / this.zoomStopHeight);
         this.moveZoomBar();
         this.mouseDragStart=null;
         slider.removeEventListener(MouseEvent.MOUSE_MOVE, this.zoomBarDrag);

         evt.stopPropagation();
       }*/
      }

      public function moveZoomBar(e:MapEvent = null):void {
         if (e.zoomChanged) {
            var newTop:Number = ((this.map.numZoomLevels - 1) - this.map.zoom) * this.zoomStopHeight + this.startTop + 1;
            this.slider.y = newTop;
            //显示
            this.label.y = newTop - this.zoomStopHeight / 2 - 3;
            this.label.text = this.map.zoom.toString();
         }
      }

      private function zoomBarMouseOver(evt:MouseEvent):void {
         this.map.dispatchEvent(new FeatureEvent(FeatureEvent.FEATURE_OVER, null));
      }

      private function zoomBarMouseOut(evt:MouseEvent):void {
         this.map.dispatchEvent(new FeatureEvent(FeatureEvent.FEATURE_OUT, null));
      }

      //Getters and setters

      public function get zoomStopWidth():Number {
         return this._zoomStopWidth;
      }

      public function set zoomStopWidth(value:Number):void {
         this._zoomStopWidth = value;
      }

      public function get zoomStopHeight():Number {
         return this._zoomStopHeight;
      }

      public function set zoomStopHeight(value:Number):void {
         this._zoomStopHeight = value;
      }

      public function get slider():DisplayObject {
         return this._slider;
      }

      public function set slider(value:DisplayObject):void {
         this._slider = value;
      }

      public function get zoomBar():DisplayObject {
         return this._zoomBar;
      }

      public function set zoomBar(value:DisplayObject):void {
         this._zoomBar = value;
      }

      public function get startTop():Number {
         return this._startTop;
      }

      public function set startTop(value:Number):void {
         this._startTop = value;
      }

      public function get mouseDragStart():Pixel {
         return this._mouseDragStart;
      }

      public function set mouseDragStart(value:Pixel):void {
         this._mouseDragStart = value;
      }

      public function get zoomStart():Pixel {
         return this._zoomStart;
      }

      public function set zoomStart(value:Pixel):void {
         this._zoomStart = value;
      }
   }
}

